package dao;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import static org.hibernate.criterion.Example.create;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for User
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see dao.User
 * @author MyEclipse Persistence Tools
 */

public class UserDAO extends BaseHibernateDAO {
    private static final Logger log = LoggerFactory.getLogger(UserDAO.class);
    // property constants
    public static final String USERNAME = "username";
    public static final String PASSWORD = "password";
    public static final String USERTYPE = "usertype";

    public boolean save(User transientInstance) {
	return save(log, "saving User instance", transientInstance);
    }

    public boolean delete(User persistentInstance) {
	return delete(log, "deleting User instance", persistentInstance);
    }

    public User findById(java.lang.String id) {
	log.debug("getting User instance with id: " + id);
	try {
	    User instance = (User) getSession().get("dao.User", id);
	    return instance;
	} catch (RuntimeException re) {
	    log.error("get failed", re);
	    throw re;
	}
    }

    public List<User> findByExample(User instance) {
	log.debug("finding User instance by example");
	try {
	    List<User> results = (List<User>) getSession()
		    .createCriteria("dao.User").add(create(instance)).list();
	    log.debug("find by example successful, result size: "
		    + results.size());
	    return results;
	} catch (RuntimeException re) {
	    log.error("find by example failed", re);
	    throw re;
	}
    }

    public List findByProperty(String propertyName, Object value) {
	log.debug("finding User instance with property: " + propertyName
		+ ", value: " + value);
	try {
	    String queryString = "from User as model where model."
		    + propertyName + "= ?";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setParameter(0, value);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find by property name failed", re);
	    throw re;
	}
    }

    public List<User> findByUsername(Object username) {
	return findByProperty(USERNAME, username);
    }

    public List<User> findByPassword(Object password) {
	return findByProperty(PASSWORD, password);
    }

    public List<User> findByUsertype(Object usertype) {
	return findByProperty(USERTYPE, usertype);
    }

    public List findAll() {
	log.debug("finding all User instances");
	try {
	    String queryString = "from User";
	    Query queryObject = getSession().createQuery(queryString);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find all failed", re);
	    throw re;
	}
    }

    public int getUserCounts(){
	try {
	    String queryString = "select count(userid) from User";
	    Query queryObject = getSession().createQuery(queryString);
	    return ((Number)queryObject.uniqueResult()).intValue();
	} catch (RuntimeException re) {
	    log.error("find all failed", re);
	    throw re;
	}
    }
    
    public List<User> findUsersByPage(int pageNum, int pageSize) {
	log.debug("finding all User instances");
	try {
	    String queryString = "from User";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setFirstResult((pageNum - 1) * pageSize);
	    queryObject.setMaxResults(pageSize);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find all failed", re);
	    throw re;
	}
    }
    
    public User merge(User detachedInstance) {
	log.debug("merging User instance");
	try {
	    User result = (User) getSession().merge(detachedInstance);
	    log.debug("merge successful");
	    return result;
	} catch (RuntimeException re) {
	    log.error("merge failed", re);
	    throw re;
	}
    }

    public void attachDirty(User instance) {
	log.debug("attaching dirty User instance");
	try {
	    getSession().saveOrUpdate(instance);
	    log.debug("attach successful");
	} catch (RuntimeException re) {
	    log.error("attach failed", re);
	    throw re;
	}
    }
    
    public boolean update(User instance) {
	return update(log, "updating User instance", instance);
    }

    public boolean resetPassword(String userid){
	try {
	    String queryString = "update User set password='999999' where userid =?";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setString(0, userid);
	    int n = queryObject.executeUpdate();
	    return n>0?true:false;
	} catch (RuntimeException re) {
	    log.error("find all failed", re);
	    throw re;
	}
    }
    public void attachClean(User instance) {
	log.debug("attaching clean User instance");
	try {
	    getSession().lock(instance, LockMode.NONE);
	    log.debug("attach successful");
	} catch (RuntimeException re) {
	    log.error("attach failed", re);
	    throw re;
	}
    }
}